package com.vitogiovanni.tobook;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListFragment;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.vitogiovanni.tobook.entity.localeendpoint.model.ElementoMenu;
import com.vitogiovanni.tobook.entity.localeendpoint.model.Locale;
import com.vitogiovanni.tobook.entity.localeendpoint.model.Menu;
import com.vitogiovanni.tobook.utilities.ElementiMenuListViewAdapter;
import com.vitogiovanni.tobook.utilities.ElementoMenuItem;


	/**
	 * A placeholder fragment containing a simple view.
	 */
	public class LocaleMenuFragment extends ListFragment{
		
		private Locale locale;
		private View rootView;
		private ListView listView;
		private ElementiMenuListViewAdapter adapter;
		private List<ElementoMenuItem> elementiItemMenu = new ArrayList<ElementoMenuItem>();
		private boolean elementiMenuEsistenti = false;	
		/**
		 * The fragment argument representing the section number for this
		 * fragment.
		 */
		private static final String ARG_SECTION_NUMBER = "section_number";

		/**
		 * Returns a new instance of this fragment for the given section number.
		 */
		public static LocaleMenuFragment newInstance(int sectionNumber, Locale locale) {
			LocaleMenuFragment fragment = new LocaleMenuFragment(locale);
			Bundle args = new Bundle();
			args.putInt(ARG_SECTION_NUMBER, sectionNumber);
			fragment.setArguments(args);
			return fragment;
		}

		public LocaleMenuFragment(Locale locale) {
			this.locale = locale;
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			rootView = inflater.inflate(R.layout.fragment_locale_menu,
					container, false);

			listView = (ListView) rootView.findViewById(android.R.id.list);	        
			adapter = new ElementiMenuListViewAdapter(getActivity().getApplicationContext(),R.layout.list_item_elemento_menu, elementiItemMenu);
			listView.setAdapter(adapter);
			
			return rootView;
		}
		
		@Override
		public void onActivityCreated(Bundle savedInstanceState){
			super.onActivityCreated(savedInstanceState);
						
			Menu menu = locale.getMenu();
			List<ElementoMenu> elementiMenu = new ArrayList<ElementoMenu>();
			
			if(menu!=null){
				elementiMenu = menu.getElementiMenu();
				
				if(elementiMenu!=null && elementiMenu.size()>0){
					elementiMenuEsistenti = true;
					List<ElementoMenu> antipastiMenu = new ArrayList<ElementoMenu>();
					List<ElementoMenu> primiMenu = new ArrayList<ElementoMenu>();
					List<ElementoMenu> secondiMenu = new ArrayList<ElementoMenu>();
					List<ElementoMenu> contorniMenu = new ArrayList<ElementoMenu>();
					List<ElementoMenu> dolciMenu = new ArrayList<ElementoMenu>();
					List<ElementoMenu> fruttaMenu = new ArrayList<ElementoMenu>();
					List<ElementoMenu> bevandeMenu = new ArrayList<ElementoMenu>();
					List<ElementoMenu> pizzaMenu = new ArrayList<ElementoMenu>();

					for(ElementoMenu elem: elementiMenu){
						String tipoElem = elem.getTipoElemento().getNome();
						if(tipoElem.equalsIgnoreCase("Antipasto")){
							antipastiMenu.add(elem);
						}
						else if(tipoElem.equalsIgnoreCase("Primo")){
							primiMenu.add(elem);
						}
						else if(tipoElem.equalsIgnoreCase("Secondo")){
							secondiMenu.add(elem);
						}
						else if(tipoElem.equalsIgnoreCase("Pizza")){
							pizzaMenu.add(elem);
						}
						else if(tipoElem.equalsIgnoreCase("Contorno")){
							contorniMenu.add(elem);
						}
						else if(tipoElem.equalsIgnoreCase("Dolce")){
							dolciMenu.add(elem);
						}
						else if(tipoElem.equalsIgnoreCase("Frutta")){
							fruttaMenu.add(elem);
						}
						else if(tipoElem.equalsIgnoreCase("Bevanda")){
							bevandeMenu.add(elem);
						}
						else{
						}
					}	
					
					if (antipastiMenu.size() > 0) {
						elementiItemMenu.add(new ElementoMenuItem((long) -1,
								"ANTIPASTI", "", "", "", 0, true));
						for (ElementoMenu elem : antipastiMenu) {
							List<String> catElemento = new ArrayList<String>();
							if (elem.getCategoriaElemento() != null) {
								for (int j = 0; j < elem.getCategoriaElemento()
										.size(); j++) {
									catElemento.add(elem.getCategoriaElemento()
											.get(j).getName());
								}
							}
							String categoriaElemento = TextUtils.join("\n",
									catElemento);
							elementiItemMenu.add(new ElementoMenuItem(elem.getId()
									.getId(), elem.getNome(),
									elem.getIngredienti(), elem.getTipoElemento()
											.getNome(), categoriaElemento, elem
											.getPrezzoElemento(), false));
						}
					}
	
					if (primiMenu.size() > 0) {
						elementiItemMenu.add(new ElementoMenuItem((long) -2,
								"PRIMI", "", "", "", 0, true));
						for (ElementoMenu elem : primiMenu) {
							List<String> catElemento = new ArrayList<String>();
							if (elem.getCategoriaElemento() != null) {
								for (int j = 0; j < elem.getCategoriaElemento()
										.size(); j++) {
									catElemento.add(elem.getCategoriaElemento()
											.get(j).getName());
								}
							}
							String categoriaElemento = TextUtils.join("\n",
									catElemento);
							elementiItemMenu.add(new ElementoMenuItem(elem.getId()
									.getId(), elem.getNome(),
									elem.getIngredienti(), elem.getTipoElemento()
											.getNome(), categoriaElemento, elem
											.getPrezzoElemento(), false));
						}
					}
	
					if (secondiMenu.size() > 0) {
						elementiItemMenu.add(new ElementoMenuItem((long) -3,
								"SECONDI", "", "", "", 0, true));
						for (ElementoMenu elem : secondiMenu) {
							List<String> catElemento = new ArrayList<String>();
							if (elem.getCategoriaElemento() != null) {
								for (int j = 0; j < elem.getCategoriaElemento()
										.size(); j++) {
									catElemento.add(elem.getCategoriaElemento()
											.get(j).getName());
								}
							}
							String categoriaElemento = TextUtils.join("\n",
									catElemento);
							elementiItemMenu.add(new ElementoMenuItem(elem.getId()
									.getId(), elem.getNome(),
									elem.getIngredienti(), elem.getTipoElemento()
											.getNome(), categoriaElemento, elem
											.getPrezzoElemento(), false));
						}
					}
	
					if (pizzaMenu.size() > 0) {
						elementiItemMenu.add(new ElementoMenuItem((long) -4,
								"PIZZE", "", "", "", 0, true));
						for (ElementoMenu elem : pizzaMenu) {
							List<String> catElemento = new ArrayList<String>();
							if (elem.getCategoriaElemento() != null) {
								for (int j = 0; j < elem.getCategoriaElemento()
										.size(); j++) {
									catElemento.add(elem.getCategoriaElemento()
											.get(j).getName());
								}
							}
							String categoriaElemento = TextUtils.join("\n",
									catElemento);
							elementiItemMenu.add(new ElementoMenuItem(elem.getId()
									.getId(), elem.getNome(),
									elem.getIngredienti(), elem.getTipoElemento()
											.getNome(), categoriaElemento, elem
											.getPrezzoElemento(), false));
						}
					}
	
					if (contorniMenu.size() > 0) {
						elementiItemMenu.add(new ElementoMenuItem((long) -5,
								"CONTORNI", "", "", "", 0, true));
						for (ElementoMenu elem : contorniMenu) {
							List<String> catElemento = new ArrayList<String>();
							if (elem.getCategoriaElemento() != null) {
								for (int j = 0; j < elem.getCategoriaElemento()
										.size(); j++) {
									catElemento.add(elem.getCategoriaElemento()
											.get(j).getName());
								}
							}
							String categoriaElemento = TextUtils.join("\n",
									catElemento);
							elementiItemMenu.add(new ElementoMenuItem(elem.getId()
									.getId(), elem.getNome(),
									elem.getIngredienti(), elem.getTipoElemento()
											.getNome(), categoriaElemento, elem
											.getPrezzoElemento(), false));
						}
					}
	
					if (dolciMenu.size() > 0) {
						elementiItemMenu.add(new ElementoMenuItem((long) -6,
								"DOLCI", "", "", "", 0, true));
						for (ElementoMenu elem : dolciMenu) {
							List<String> catElemento = new ArrayList<String>();
							if (elem.getCategoriaElemento() != null) {
								for (int j = 0; j < elem.getCategoriaElemento()
										.size(); j++) {
									catElemento.add(elem.getCategoriaElemento()
											.get(j).getName());
								}
							}
							String categoriaElemento = TextUtils.join("\n",
									catElemento);
							elementiItemMenu.add(new ElementoMenuItem(elem.getId()
									.getId(), elem.getNome(),
									elem.getIngredienti(), elem.getTipoElemento()
											.getNome(), categoriaElemento, elem
											.getPrezzoElemento(), false));
						}
					}
	
					if (fruttaMenu.size() > 0) {
						elementiItemMenu.add(new ElementoMenuItem((long) -7,
								"FRUTTA", "", "", "", 0, true));
						for (ElementoMenu elem : fruttaMenu) {
							List<String> catElemento = new ArrayList<String>();
							if (elem.getCategoriaElemento() != null) {
								for (int j = 0; j < elem.getCategoriaElemento()
										.size(); j++) {
									catElemento.add(elem.getCategoriaElemento()
											.get(j).getName());
								}
							}
							String categoriaElemento = TextUtils.join("\n",
									catElemento);
							elementiItemMenu.add(new ElementoMenuItem(elem.getId()
									.getId(), elem.getNome(),
									elem.getIngredienti(), elem.getTipoElemento()
											.getNome(), categoriaElemento, elem
											.getPrezzoElemento(), false));
						}
					}
	
					if (bevandeMenu.size() > 0) {
						elementiItemMenu.add(new ElementoMenuItem((long) -8,
								"BEVANDE", "", "", "", 0, true));
						for (ElementoMenu elem : bevandeMenu) {
							List<String> catElemento = new ArrayList<String>();
							if (elem.getCategoriaElemento() != null) {
								for (int j = 0; j < elem.getCategoriaElemento()
										.size(); j++) {
									catElemento.add(elem.getCategoriaElemento()
											.get(j).getName());
								}
							}
							String categoriaElemento = TextUtils.join("\n",
									catElemento);
							elementiItemMenu.add(new ElementoMenuItem(elem.getId()
									.getId(), elem.getNome(),
									elem.getIngredienti(), elem.getTipoElemento()
											.getNome(), categoriaElemento, elem
											.getPrezzoElemento(), false));
						}
					}
					
					adapter.notifyDataSetChanged();
				}
				else{
					elementiMenuEsistenti = false;
				}
			}		
		}

		@Override
		public void onListItemClick(ListView l, View v, int position, long id) {
			super.onListItemClick(l, v, position, id);
			
			AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
			
			TextView textTitle = new TextView(getActivity().getApplicationContext());
			textTitle.setTextSize(28);
			textTitle.setTextColor(Color.DKGRAY);
			textTitle.setText(adapter.getItem(position).getNome().toUpperCase());
			textTitle.setPadding(20, 20, 20, 20);
			
			String categorieElemento = adapter.getItem(position).getCategoria();
			String ingredientiElemento = "";
			if(adapter.getItem(position).getIngredienti() != null){
				ingredientiElemento = adapter.getItem(position).getIngredienti().toLowerCase();
			}
			String prezzoElemento = adapter.getItem(position).getPrezzo() + " " + getResources().getString(R.string.euro);
			
			TextView textIngredienti = new TextView(getActivity().getApplicationContext());
			textIngredienti.setTextSize(20);
			textIngredienti.setTextColor(Color.DKGRAY);
			textIngredienti.setPadding(20, 20, 20, 20);
			String message = getResources().getString(R.string.ingredienti) + ":\n" + ingredientiElemento
					+ "\n\n" + getResources().getString(R.string.categorie) + ":\n" + categorieElemento
					+ "\n\n" + getResources().getString(R.string.prezzo) + ":\n" + prezzoElemento;
			textIngredienti.setText(message);
			
			builder.setCustomTitle(textTitle)
				.setView(textIngredienti)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.dismiss();
					}
				});
			       

			AlertDialog dialog = builder.create();
			dialog.show();
		
		}
		
		@Override
		public void setUserVisibleHint(boolean isVi){
			super.setUserVisibleHint(isVi);
			if(isVi && !elementiMenuEsistenti){
				Toast.makeText(getActivity().getApplicationContext(), R.string.noMenu, Toast.LENGTH_SHORT).show();
			}
		}
		
	}